package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.sms.SmsHomeRecommendProductMapper;
import com.itheima.pojo.sms.SmsHomeRecommendProduct;
import com.itheima.pojo.sms.vo.UpdateVo;
import com.itheima.service.sms.SmsHomeRecommendProductService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SmsHomeRecommendProductServiceImpl extends ServiceImpl<SmsHomeRecommendProductMapper, SmsHomeRecommendProduct> implements SmsHomeRecommendProductService {



    /**
     * 推荐分页查询
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @Override
    public PageInfo pageList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        // Page<SmsHomeRecommendProduct> pageInfo=new Page<>(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(recommendStatus!=null,SmsHomeRecommendProduct::getRecommendStatus,recommendStatus)
                .like(productName!=null,SmsHomeRecommendProduct::getProductName,productName)
                .orderByDesc(SmsHomeRecommendProduct::getSort);
        //Page<SmsHomeRecommendProduct> page = this.page(pageInfo, lambdaQueryWrapper);
        List<SmsHomeRecommendProduct> list = this.list(lambdaQueryWrapper);
        pageInfo.setList(list);
        int total = this.count(lambdaQueryWrapper);
        pageInfo.setTotal(total);
        Integer totalPage = PageInfo.totalPage(pageSize, total);
        pageInfo.setTotalPage(totalPage);

        return pageInfo;

    }

    /**
     * 批量修改推荐状态
     * @param
     */
    @Override
    public void updateStatus(List<Long> ids,  Integer recommendStatus) {
       ids.stream().forEach(item->{
           SmsHomeRecommendProduct recommendProduct = this.getById(item);
           recommendProduct.setRecommendStatus(recommendStatus);
           this.updateById(recommendProduct);
       });

    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = this.getById(id);
        if (smsHomeRecommendProduct!=null){
        smsHomeRecommendProduct.setSort(sort);
        this.updateById(smsHomeRecommendProduct);
        }
    }
}
